<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//用户体验平台记录表
class Plat_award_list_model extends CI_Model{
    function __construct() {
        parent::__construct();
        $this->table = 'plat_award_list';
        $this->users = 'users';
        $this->users_info = 'users_info';
        $this->plat = "plat";
    }
    
    function getCount($where){
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where($where);
        $this->db->order_by('create_time','desc');
        $query = $this->db->get();
        return $query->num_rows();
    }
    
    function getList($where,$page,$limit){
        $this->db->select($this->table.".*,".$this->plat.'.name_ch,'.$this->plat.'.is_show,'.$this->plat.'.name_en,'.$this->users.".username,".$this->plat.".plat_period");
        $this->db->from($this->table);
        $this->db->join($this->plat, $this->plat.'.id = '.$this->table.'.plat_id', 'left');
        $this->db->join($this->users, $this->users.'.user_id = '.$this->table.'.user_id', 'left');
        $this->db->where($where);
        $this->db->order_by('create_time','desc');
        $this->db->limit($limit, $page);
        return $this->db->get()->result_array();
    }
    
    function get($where){
        return $this->db->select('*')->from($this->table)->where($where)->order_by('create_time desc')->get()->row_array();
    }
    
    function get_list($where){
        return $this->db->select('*')->from($this->table)->where($where)->order_by('create_time desc')->get()->result_array();
    }
    
    function get_expers_list($where,$page,$limit){
        $this->db->select($this->table.".*,".$this->plat.'.name_ch,'.$this->users.'.username');
        $this->db->from($this->table);
        $this->db->join($this->plat, $this->plat.'.id = '.$this->table.'.plat_id', 'left');
        $this->db->join($this->users, $this->users.'.user_id = '.$this->table.'.user_id', 'left');
        $this->db->where($where);
        $this->db->order_by('create_time','desc');
        $this->db->limit($limit, $page);
        return $this->db->get()->result_array();
    }
    //获取用户试玩平台排行数据
    public function getUsersAmountInfo($where = array(),$page,$limit){
        $this->db->select('SUM(award) as total_award,'.$this->users.'.username,'.$this->users_info.'.nickname');
        $this->db->from($this->table);
        $this->db->join($this->users, $this->users.'.user_id = '.$this->table.'.user_id', 'left');
        $this->db->join($this->users_info, $this->users_info.'.user_id = '.$this->table.'.user_id', 'left');
        $this->db->where($where);
        $this->db->group_by($this->table.'.user_id');
        $this->db->order_by("total_award desc");
        $this->db->limit($limit, $page);
        $query = $this->db->get();
        return $query->result_array();
        
    }
    
    function getTotal($where){
        $this->db->select('SUM(award) award');
        $this->db->from($this->table);
        $this->db->where($where);
        $this->db->order_by('id','desc');
        return $this->db->get()->row_array();
    }
    
    function insert($data){
        $this->db->insert($this->table, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    
    function update($data,$id){
        return $this->db->where('id',$id)->update($this->table, $data);
    }
    
    function get_raward_no_list($where,$page,$limit){
        $this->db->select($this->table.".*");
        $this->db->from($this->table);
        $this->db->where($where);
        $this->db->order_by('create_time','desc');
        $this->db->limit($limit, $page);
        return $this->db->get()->result_array();
    }
}
